package com.zfzs.post.common.bean;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 错误信息响应体
 *
 * @author: ck
 * @create: 2019/6/3/14:48
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ResponseResult<T> implements Serializable {


    private String code;

    private Object data ;

    private String msg;

    private final long timestamps = System.currentTimeMillis();


    public synchronized static <T> ResponseResult<T> ok() {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResponseCode.SUCCESS.code);
        res.setMsg(ResponseCode.SUCCESS.msg);
        Map<String,Object> map = new HashMap<>();
        res.setData(map);
        return res;
    }

    public synchronized static <T> ResponseResult<T> ok(T data) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResponseCode.SUCCESS.code);
        res.setMsg(ResponseCode.SUCCESS.msg);
        res.setData(data);
        return res;
    }

    public synchronized static <T> ResponseResult<T> okMsg(String msg) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResponseCode.SUCCESS.code);
        res.setMsg(msg);
        Map<String,Object> map = new HashMap<>();
        res.setData(map);
        return res;
    }

    public synchronized static <T> ResponseResult<T> ok(String msg, T data) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResponseCode.SUCCESS.code);
        res.setMsg(msg);
        res.setData(data);
        return res;
    }

    //系统异常错误提示
    public synchronized static <T> ResponseResult<T> error() {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(ResponseCode.SERVER_EXCEPTION_ERROR.code);
        res.setMsg(ResponseCode.SERVER_EXCEPTION_ERROR.msg);
        Map<String,Object> map = new HashMap<>();
        res.setData(map);
        return res;
    }


    public synchronized static <T> ResponseResult<T> error(ResponseCode statusEnum) {
        return error(statusEnum, null);
    }

    public synchronized static <T> ResponseResult<T> error(ResponseCode statusEnum, T data) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(statusEnum.code);
        res.setMsg(statusEnum.msg);
        Map<String,Object> map = new HashMap<>();
        map.put("error",data);
        res.setData(map);
        return res;
    }

    public synchronized static <T> ResponseResult<T> error(ResponseCode statusEnum, String msg) {
        ResponseResult<T> res = new ResponseResult<>();
        res.setCode(statusEnum.code);
        res.setMsg(msg);
        Map<String,Object> map = new HashMap<>();
        res.setData(map);
        return res;
    }

    private static final long serialVersionUID = 8992436576262574064L;
}
